package ilvonen.ihstatistics.repository;

import ilvonen.ihstatistics.models.Player;
import ilvonen.ihstatistics.models.Team;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;

public class DBPlayers {
	//define table name
		private static final String TABLE_NAME = "players";
	//define tables column names
		private static final String KEY_ID = "_id";
		private static final String KEY_ROLE = "role_id";
		private static final String KEY_ACTIVE = "active";
		private static final String KEY_FNAME = "firstname";
		private static final String KEY_LNAME = "lastname";
		private static final String KEY_SLEEVENO = "sleeve_number";
		private static final String KEY_HEIGHT = "height";
		private static final String KEY_WEIGHT = "weight";
		private static final String KEY_SHOOTS = "shoots";
		private static final String KEY_BDAY = "birthday";
		private static final String KEY_BPLACE = "birthplace";
	//define create statement for all your tables. Create new file (class) for each table.
		private static final String CREATE_STATEMENT = "create table if not exists " + TABLE_NAME + " (" 
														+ KEY_ID + " integer primary key autoincrement, "
														+ KEY_ROLE + " integer not null, "
														+ KEY_ACTIVE + " integer not null, "
														+ KEY_FNAME + " text not null, "
														+ KEY_LNAME + " text not null, "
														+ KEY_SLEEVENO + " integer not null, "
														+ KEY_HEIGHT + " text not null, "
														+ KEY_WEIGHT + " text not null, "
														+ KEY_SHOOTS + " text not null, "
														+ KEY_BDAY + " text not null, "
														+ KEY_BPLACE + " text not null);";
	
	/*
	 * CLASS STATUS: READY?YES
	 */
	public DBPlayers(){}
	/*
	 * 
	 */
	public String create(){
		return CREATE_STATEMENT;
	}
	/*
	 * 
	 */
	public long insert(String fname, String lname, int role, int active, int sleeveno, String height, String weight, String shoots, String bday, String bplace, DBAdapter db){
		
		ContentValues initialValues = new ContentValues();
		initialValues.put(KEY_ROLE, role);
		initialValues.put(KEY_ACTIVE, active);
		initialValues.put(KEY_FNAME, fname);
		initialValues.put(KEY_LNAME, lname);
		initialValues.put(KEY_SLEEVENO, sleeveno);
		initialValues.put(KEY_HEIGHT, height);
		initialValues.put(KEY_WEIGHT, weight);
		initialValues.put(KEY_SHOOTS, shoots);
		initialValues.put(KEY_BDAY, bday);
		initialValues.put(KEY_BPLACE, bplace);
		
		return db.insertRecord(TABLE_NAME, initialValues);
		
	}
	/*
	 * 
	 */
	public boolean delete(long rowId, DBAdapter db){
		return db.deleteRecord(TABLE_NAME, KEY_ID, rowId);
	}
	/*
	 * Doesn't matter wheter the player is a skater or a goalie. It will be defined later elsewhere.
	 */
	public List<Player> getAllPlayers(DBAdapter db){
		Cursor c;
		List<Player> players = new ArrayList<Player>();
		c = db.getAllRecords(TABLE_NAME, new String[]{KEY_ID,KEY_ROLE,KEY_ACTIVE,KEY_FNAME,KEY_LNAME,KEY_SLEEVENO,KEY_HEIGHT,KEY_WEIGHT,KEY_SHOOTS,KEY_BDAY,KEY_BPLACE});
		while(c.moveToNext()){
			Player p = new Player();
			p.set_id(c.getInt(0));
			p.set_role(c.getInt(1));
			p.set_active(c.getInt(2));
			p.set_firstname(c.getString(3));
			p.set_lastname(c.getString(4));
			p.set_sleeveNumber(c.getInt(5));
			p.set_height(c.getString(6));
			p.set_weight(c.getString(7));
			p.set_shoots(c.getString(8));
			p.set_birthday(c.getString(9));
			p.set_birthplace(c.getString(10));
			players.add(p);
		}
		return players; //list of all players in the database
	}
	/*
	 * 
	 */
	public List<Player> getAllPlayersByActivity(DBAdapter db, long ROW_ID){
		Cursor c;
		List<Player> players = new ArrayList<Player>();
		c = db.getAllRecordsById(TABLE_NAME, new String[]{KEY_ID,KEY_ROLE,KEY_ACTIVE,KEY_FNAME,KEY_LNAME,KEY_SLEEVENO,KEY_HEIGHT,KEY_WEIGHT,KEY_SHOOTS,KEY_BDAY,KEY_BPLACE}, KEY_ACTIVE, ROW_ID);
		while(c.moveToNext()){
			Player p = new Player();
			p.set_id(c.getInt(0));
			p.set_role(c.getInt(1));
			p.set_active(c.getInt(2));
			p.set_firstname(c.getString(3));
			p.set_lastname(c.getString(4));
			p.set_sleeveNumber(c.getInt(5));
			p.set_height(c.getString(6));
			p.set_weight(c.getString(7));
			p.set_shoots(c.getString(8));
			p.set_birthday(c.getString(9));
			p.set_birthplace(c.getString(10));
			players.add(p);
		}
		return players;
	}
	public Player getPlayerById(DBAdapter db, long ROW_ID){
		Player p = null;
		Cursor c;
		c = db.getRecord(TABLE_NAME, new String[]{KEY_ID,KEY_ROLE,KEY_ACTIVE,KEY_FNAME,KEY_LNAME,KEY_SLEEVENO,KEY_HEIGHT,KEY_WEIGHT,KEY_SHOOTS,KEY_BDAY,KEY_BPLACE}, KEY_ID, ROW_ID);
		if(c != null){
			p = new Player();
			c.moveToFirst();
			p.set_id(c.getInt(0));
			p.set_role(c.getInt(1));
			p.set_active(c.getInt(2));
			p.set_firstname(c.getString(3));
			p.set_lastname(c.getString(4));
			p.set_sleeveNumber(c.getInt(5));
			p.set_height(c.getString(6));
			p.set_weight(c.getString(7));
			p.set_shoots(c.getString(8));
			p.set_birthday(c.getString(9));
			p.set_birthplace(c.getString(10));
		}
		return p;
	}
}
